<template>
	<view class="j-address">
		<template v-if="addressList.length">
			<view class="j-a-box">
				<view class="j-a-row" v-for="(item, index) in addressList" :key="index">
					<view class="" @click="selectAddress(item)">
						<view class="j-a-r-info">
							<text>{{ item.realName }}</text>
							<text>{{ item.mobile }}</text>
						</view>
						<view class="j-a-r-address">{{ item.fullName }}</view>
					</view>

					<view class="j-a-r-def">
						<view class="j-a-r-d">
							<u-radio-group v-model="item.isDefault" @change="postSetDefaultAddress(item)"><u-radio
									shape="circle" name="1" active-color="#FEA602">默认地址</u-radio></u-radio-group>
						</view>
						<view class="j-a-r-o">
							<view class="j-a-r-o-edit" @click="toEditAddress(item)">编辑</view>
							<view class="j-a-r-o-del" @click="showDelAddress(item)">删除</view>
						</view>
					</view>
				</view>
			</view>
		</template>
		<template v-else>
			<view class="j-a-no"><u-empty mode="address"></u-empty>
			</view>
		</template>
		<view class="shdz" @click="toEditAddress">
			新增收货地址
		</view>
		<u-modal v-model="isShowDelAddress" ref="delAddress" content="确定删除当前地址？" :show-cancel-button="true"
			:async-close="true" @confirm="postDeleteUserAddress"></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isShowDelAddress: false,
				checked: '0',
				addressList: [],
				userAddressId: '',
				params: {
					page: 1,
					rows: 1000000
				},
				type: ''
			};
		},
		onLoad(e) {
			if (e.type == 1) {
				// 1:代表下单页面打开选择地址
				this.type = e.type;
			}
		},
		onShow() {
			// 查询地址列表
			this.postFindAddressList();
		},
		onNavigationBarButtonTap() {
			// 去添加地址
			this.toEditAddress();
		},
		methods: {
			// 选择地址并返回上一个页面
			selectAddress(item) {
				// 下单页面处理地址数据
				if (this.type == 1) {
					// console.log(1)
					uni.$emit('addressInfo', item);
					uni.navigateBack();
				}
			},
			// 查询地址列表
			postFindAddressList() {
				this.$u.api
					.postFindAddressList(this.params)
					.then(res => {
						console.log(res, '[收货地址列表页-查询地址列表接口]');
						this.addressList = res.data.rows;

					})
					.catch(err => {
						console.log(err);

					});
			},
			// 设为默认收货地址
			postSetDefaultAddress(item) {
				// this.$u.api['active/getActiveInfo']({ id: '/pages/active/lottery/lottery' }).then(res => {
				// 	this.prizes = res.prizeList;
				// 	this.activeInfo = res;
				// });
				this.$u.api['address/postSetDefaultAddress']({
						userAddressId: item.userAddressId
					})
					.then(res => {
						this.postFindAddressList();
						this.$refs.uToast.show({
							title: '设置默认地址成功'
						});
						this.isShowDelAddress = false;
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 显示询问是否删除当前地址
			showDelAddress(item) {
				this.userAddressId = item.userAddressId;
				this.isShowDelAddress = true;
			},
			// 删除地址
			postDeleteUserAddress() {
				this.$u.api
					.postDeleteUserAddress({
						userAddressId: this.userAddressId
					})
					.then(res => {
						this.postFindAddressList();
						this.$refs.uToast.show({
							title: '删除地址成功'
						});
						this.isShowDelAddress = false;
					})
					.catch(err => {
						console.log(err);
						this.$refs.delAddress.clearLoading();
					});
			},
			// 跳转去添加或编辑地址页面
			toEditAddress(item) {
				let addressInfo = '';
				if (item.realName) {
					item.isAdd = false
				} else {
					item.isAdd = true
				}
				if (item) {
					addressInfo = JSON.stringify(item)
				}
				uni.navigateTo({
					url: './address-add?addressInfo=' + addressInfo
				});

			}
		}
	};
</script>

<style>
	page {
		background-color: #f3f3f3;
	}
</style>
<style scoped lang="scss">
	.shdz {
		width: 80vw;
		height: 5vh;
		position: relative;
		// bottom: 20vh;
		text-align: center;
		background: #003B40;
		color: #fff;
		left: 10vw;
		line-height: 5vh;
		border-radius: 30px;
	}

	.j-address {
		.j-a-no {
			margin-top: 200rpx;
		}

		.j-a-box {
			padding: 20rpx 25rpx;

			.j-a-row {
				margin-top: 20rpx;
				border-radius: 14rpx;
				background-color: #ffffff;

				&:first-of-type {
					margin-top: 0;
				}

				.j-a-r-info {
					display: flex;
					align-items: center;
					padding: 25rpx;
					justify-content: space-between;
				}

				.j-a-r-address {
					color: #666666;
					padding: 0 25rpx 25rpx 25rpx;
					border-bottom: 1rpx solid #f1f1f1;
				}

				.j-a-r-def {
					display: flex;
					padding: 25rpx;
					align-items: center;
					justify-content: space-between;

					.j-a-r-o {
						display: flex;

						.j-a-r-o-edit {
							margin-right: 38rpx;
						}
					}
				}
			}
		}
	}
</style>